using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using TagTool.Devices.Tablet;
using TagTool.Devices;
using TagTool.Devices.DataStructures;
using TagTool.Devices.Joystick;

namespace TagTool.TabletTest
{
    public partial class FormMain : Form
    {
        private TabletManager manager;
    	private ITabletDevice tablet;
        private JoystickManager joyManager;
        private JoystickDevice joy;
        private uint deviceId;

        private JoystickExtInfo infoExt;

        public FormMain()
        {
            InitializeComponent();
            listBoxTablet.ContextMenuStrip = contextMenu;
            manager = new TabletManager();
            uint nDevices = manager.GetIInfoNumberOfDevices();
            listBoxTablet.Items.Add(string.Format("Number of tablets: {0}", nDevices));
            if (nDevices > 0)
            {
                tablet = (WinTablet)manager.GetDevice(0);
                tablet.DefaultContext.CursorDeviceEvent += new CursorDeviceEventHandler(OnCursorEvent);
            }

            joyManager = new JoystickManager();
            joy = (JoystickDevice)joyManager.GetDevice(0);
            joy.JoystickEvent += new JoystickEventHandler(OnJoystickEvent);
            joy.Connect();
        }

        private void OnJoystickEvent(object sender, JoystickDeviceEventArgs e)
        {
            listBoxTablet.Items.Add(string.Format("Joystick X: {0:f2}, Y: {1:f2}, Y: {2:f2}, Buttons: {3}", e.X, e.Y, e.Z, e.Buttons));
            listBoxTablet.TopIndex = listBoxTablet.Items.Count - 1;
        }


        private void openTablet_Click(object sender, EventArgs e)
        {
            listBoxTablet.Items.Add(string.Format("Opening device {0}", tablet.DeviceId));
            tablet.Connect();
            Console.WriteLine("Enabled = " + tablet.DefaultContext.Enabled);
        }

        private void closeTablet_Click(object sender, EventArgs e)
        {
        	tablet.Disconnect();
            listBoxTablet.Items.Add(string.Format("Closed device {0}", tablet.DeviceId));
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
        	CloseTablet();
            WinJoystick.ReleaseCapture(deviceId);
        }

        private void CloseTablet()
        {
            if (tablet != null)
            {
                tablet.Disconnect();
            }
        }

        private void getExtensions_Click(object sender, EventArgs e)
        {
        }

        private void OnCursorEvent(object sender, CursorDeviceEventArgs e)
        {
            listBoxTablet.Items.Add(string.Format("Device: {0}, Context: {1}, X: {2:f2}, Y: {3:f2}, Pressure {4}, Orientation: {5}", e.DeviceId, e.Context, e.X, e.Y, e.NormalPressure, e.Orientation));
            listBoxTablet.TopIndex = listBoxTablet.Items.Count - 1;

        }

        private void OnTabletEvent(object sender, TabletEventArgs e)
        {
            listBoxTablet.Items.Add(string.Format("Message: {0}, Context: {1}", e.Message, e.Context));
            listBoxTablet.TopIndex = listBoxTablet.Items.Count - 1;
        }

        private void splitContextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tablet.SplitContext(50);
        }

        private void createMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tablet.CreatePaperMenu(0, 20, 10, 1, 5);
        }
    }
}
